package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_routing_workcenter;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_routing_workcenterSearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_routing_workcenterService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[工作中心使用情况] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_routing_workcenterServiceImpl implements IMrp_routing_workcenterService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_routing_workcenterClientService mrp_routing_workcenterClientService;

    @Override
    public void createBatch(List<Mrp_routing_workcenter> mrp_routing_workcenters, int batchSize) {

                        //mrp_routing_workcenterClientService.createBatch(mrp_routing_workcenters,batchSize) ;

    }

    @Override
    public void updateBatch(List<Mrp_routing_workcenter> mrp_routing_workcenters, int batchSize) {

        //mrp_routing_workcenterClientService.updateBatch(mrp_routing_workcenters,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_routing_workcenter clientModel = mrp_routing_workcenterClientService.createModel();
        clientModel.setId(id);
        mrp_routing_workcenterClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Mrp_routing_workcenter mrp_routing_workcenter) {
        boolean bOk = false ;
        Imrp_routing_workcenter clientModel = this.convert2Model(mrp_routing_workcenter,null);
        mrp_routing_workcenterClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_routing_workcenter);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mrp_routing_workcenter mrp_routing_workcenter) {
        boolean bOk = false ;
        Imrp_routing_workcenter clientModel = this.convert2Model(mrp_routing_workcenter,null);
        mrp_routing_workcenterClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_routing_workcenter) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_routing_workcenterClientService.removeBatch(mrp_routing_workcenters) ;

    }

    @Override
    public Mrp_routing_workcenter get(Integer id) {
        Imrp_routing_workcenter clientModel = mrp_routing_workcenterClientService.createModel();
        clientModel.setId(id);
        mrp_routing_workcenterClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_routing_workcenter get(Mrp_routing_workcenter mrp_routing_workcenter) {
        Mrp_routing_workcenter retmrp_routing_workcenter = this.get(mrp_routing_workcenter.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_routing_workcenter.getClass(), mrp_routing_workcenter.getClass(), false);
        copier.copy(retmrp_routing_workcenter, mrp_routing_workcenter, null);
        return mrp_routing_workcenter ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_routing_workcenter> searchDefault(Mrp_routing_workcenterSearchContext context) {
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据工艺查询
     */
    @Override
    public Page<Mrp_routing_workcenter> selectByOdoo_routing(Integer id){
        Mrp_routing_workcenterSearchContext context = new Mrp_routing_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据工作中心查询
     */
    @Override
    public Page<Mrp_routing_workcenter> selectByOdoo_workcenter(Integer id){
        Mrp_routing_workcenterSearchContext context = new Mrp_routing_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Mrp_routing_workcenter> selectByOdoo_company(Integer id){
        Mrp_routing_workcenterSearchContext context = new Mrp_routing_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_routing_workcenter> selectByOdoo_create(Integer id){
        Mrp_routing_workcenterSearchContext context = new Mrp_routing_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_routing_workcenter> selectByOdoo_write(Integer id){
        Mrp_routing_workcenterSearchContext context = new Mrp_routing_workcenterSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_routing_workcenter> list = new ArrayList<Mrp_routing_workcenter>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter> clientModelList = mrp_routing_workcenterClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_routing_workcenter clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_routing_workcenter>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_routing_workcenter convert2Model(Mrp_routing_workcenter domain , Imrp_routing_workcenter model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_routing_workcenterClientService.createModel() ;
        }

         if(domain.getTime_mode_batchDirtyFlag())
            model.setTime_mode_batch(domain.getTime_mode_batch());
         if(domain.getBatch_sizeDirtyFlag())
            model.setBatch_size(domain.getBatch_size());
         if(domain.getWorkorder_idsDirtyFlag())
            model.setWorkorder_ids(domain.getWorkorder_ids());
         if(domain.getWorkorder_countDirtyFlag())
            model.setWorkorder_count(domain.getWorkorder_count());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getBatchDirtyFlag())
            model.setBatch(domain.getBatch());
         if(domain.getTime_cycle_manualDirtyFlag())
            model.setTime_cycle_manual(domain.getTime_cycle_manual());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getTime_modeDirtyFlag())
            model.setTime_mode(domain.getTime_mode());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getTime_cycleDirtyFlag())
            model.setTime_cycle(domain.getTime_cycle());
         if(domain.getWorksheetDirtyFlag())
            model.setWorksheet(domain.getWorksheet());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getWorkcenter_id_textDirtyFlag())
            model.setWorkcenter_id_text(domain.getWorkcenter_id_text());
         if(domain.getRouting_id_textDirtyFlag())
            model.setRouting_id_text(domain.getRouting_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getRouting_idDirtyFlag())
            model.setRouting_id(domain.getRouting_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getWorkcenter_idDirtyFlag())
            model.setWorkcenter_id(domain.getWorkcenter_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Mrp_routing_workcenter convert2Domain( Imrp_routing_workcenter model ,Mrp_routing_workcenter domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_routing_workcenter();
        }

        if(model.getTime_mode_batchDirtyFlag())
            domain.setTime_mode_batch(model.getTime_mode_batch());
        if(model.getBatch_sizeDirtyFlag())
            domain.setBatch_size(model.getBatch_size());
        if(model.getWorkorder_idsDirtyFlag())
            domain.setWorkorder_ids(model.getWorkorder_ids());
        if(model.getWorkorder_countDirtyFlag())
            domain.setWorkorder_count(model.getWorkorder_count());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getBatchDirtyFlag())
            domain.setBatch(model.getBatch());
        if(model.getTime_cycle_manualDirtyFlag())
            domain.setTime_cycle_manual(model.getTime_cycle_manual());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getTime_modeDirtyFlag())
            domain.setTime_mode(model.getTime_mode());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getTime_cycleDirtyFlag())
            domain.setTime_cycle(model.getTime_cycle());
        if(model.getWorksheetDirtyFlag())
            domain.setWorksheet(model.getWorksheet());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getWorkcenter_id_textDirtyFlag())
            domain.setWorkcenter_id_text(model.getWorkcenter_id_text());
        if(model.getRouting_id_textDirtyFlag())
            domain.setRouting_id_text(model.getRouting_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getRouting_idDirtyFlag())
            domain.setRouting_id(model.getRouting_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getWorkcenter_idDirtyFlag())
            domain.setWorkcenter_id(model.getWorkcenter_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
